import useStorage from "./useStorage.js"

// #ifdef H5
const URL = '/api';
// #endif

// #ifdef APP
const URL = "https://api.jingzuanhuicui.com/v1"
// #endif
export default function(api, data, tips = true) {
	const location = useStorage.get('location') || {};
	return new Promise((resolve, reject) => {
		uni.request({
			url: URL + api,
			data: data,
			method: 'POST',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'api-token': useStorage.get('token'),
				'appid': 'wx8e5f8f55c020bf36',
				'lnt': location.lng || '',
				'lat': location.lat || '',
			},
			success: res => {
				if (res.data.code == 1) {
					resolve(res.data.data)
				} else if (res.data.code == 403) {
					useStorage.clear()
					uni.showToast({
						title: res.data.info,
						icon: 'none',
						mask: true
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}, 1000)
				} else {
					if (tips) {
						uni.showToast({
							title: res.data.info,
							icon: 'none'
						})
					}
					reject(res.data)
				}
			},
			fail: err => {
				reject(err.data)
			}
		})
	})
}